<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='用户管理',active='user'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
</head>

<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <a class="page-title" th:href="@{/admin/user}">用户管理</a>
                        <form th:action="@{/admin/user/search}" method="get" class="search-bar" style="text-align: right;">
                            <input type="text" id="keyword" name="keyword" placeholder="请输入关键字">
                            <button type="submit" id="searchBtn">搜索</button>
                        </form>
                    </div>
                    <div class="col-md-12">
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th >姓名</th>
                                <th >密码</th>
                                <th >性别</th>
                                <th >电话</th>
                                <th>创建时间</th>
                                <th>用户权限</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="user : ${userList}">
                                <tr th:id="${user.id}">
                                    <td><th:block th:text="${user.username}"/></td>
                                    <td><th:block th:text="${user.password}"/></td>
                                    <td><th:block th:text="${user.sex}"/></td>
                                    <td><th:block th:text="${user.tel}"/></td>
                                    <td><th:block th:text="${commons.dateFormat(user.created)}"/></td>
                                    <td><th:block th:text="${user.authority}"/></td>
                                    <td>
                                        <a href="javascript:void(0)" th:onclick="'updUser('+${user.id}+');'"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-edit"></i> <span>管理员</span></a>
                                        <a href="javascript:void(0)" th:onclick="'delUser('+${user.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-trash-o"></i> <span>删除</span></a>
                                        <a href="javascript:void(0)"   th:data-user-id="${user.id}"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-rocket"></i> <span>编辑</span></a>
                                    </td>
                                </tr>
                            </th:block>
                            </tbody>
                        </table>
                        <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <!-- 模态框的内容将在此处动态加载 -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
    function delUser(id) {
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        if(confirm('确定删除该用户吗?')){
            $.ajax({
                type:'post',
                url : '/admin/user/delete',
                data: {id:id},
                dataType: 'json',
                beforeSend : function(xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        window.alert("用户删除成功");
                        window.location.reload();
                    } else {
                        window.alert(result.msg || '用户删除失败')
                    }
                }
            });
        }
    }
    function updUser(id){
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        if(confirm('确定将该用户设置为管理员吗?')){
            $.ajax({
                type:'post',
                url : '/admin/user/update',
                data: {id:id},
                dataType: 'json',
                beforeSend : function(xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        window.alert("权限更新成功");
                        window.location.reload();
                    } else {
                        window.alert(result.msg || '权限更新失败')
                    }
                }
            });
        }
    }
    // 编辑按钮的点击事件处理程序
    $(document).on("click", ".btn-primary", function() {
        var id = $(this).data("user-id");
        var url = "/admin/user/edit/" + id;

        // 使用Ajax加载编辑模态框的内容
        $.get(url, function(data) {
            $("#editModal .modal-content").html(data);
            $("#editModal").modal("show");
        });
        return true;
    });




</script>
</body>
</html>